/**
 * 
 */
package com.android.weibogo;

import java.util.ArrayList;
import java.util.List;

import com.android.weibogo.AsyncImageLoader.ImageCallback;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author xiang.jianhua
 *
 */
public class TecentWeiboAdpater extends BaseAdapter {
	
	private List<WeiboInfo> wbList;
	private AsyncImageLoader asyncImageLoader;
	private Context context;
	private int lastPageItemIdx;
	
	/**
	 * @return the lastPageItemIdx
	 */
	public int getLastPageItemIdx() {
		return lastPageItemIdx;
	}

	/**
	 * @param lastPageItemIdx the lastPageItemIdx to set
	 */
	public void setLastPageItemIdx(int lastPageItemIdx) {
		this.lastPageItemIdx = lastPageItemIdx;
	}

	/**
	 * @param wbList
	 */
	public TecentWeiboAdpater(Context ctx, List<WeiboInfo> wbList) {
		if(wbList == null)
			wbList = new ArrayList<WeiboInfo>();
		this.wbList = wbList;
		this.context = ctx;
		asyncImageLoader = new AsyncImageLoader();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return wbList.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return wbList.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(context).inflate(R.layout.tecent_weibo_list, null);
		ImageView wbIcon = (ImageView)convertView.findViewById(R.id.wbicon);
		TextView wbText = (TextView)convertView.findViewById(R.id.wbtext);
		TextView wbType = (TextView)convertView.findViewById(R.id.wbtype);
		TextView wbTime = (TextView) convertView.findViewById(R.id.wbtime);
		TextView wbUser = (TextView) convertView.findViewById(R.id.wbuser);
		ImageView wbImage = (ImageView) convertView.findViewById(R.id.wbimage);
		LinearLayout wbSourceLayout = (LinearLayout)convertView.findViewById(R.id.wbsourcelayout);
		TextView wbSourceText = (TextView)convertView.findViewById(R.id.wbsourcetext);
		ImageView wbSourceImage = (ImageView) convertView.findViewById(R.id.wbsourceimage);
		TextView wbSourceTimeAndFromText = (TextView)convertView.findViewById(R.id.wbsourcetimeandfrom);
		TecentWeiboInfo wbInfo = (TecentWeiboInfo)wbList.get(position);
		if(wbInfo != null){
			convertView.setTag(wbInfo.getId());
			wbUser.setText(wbInfo.getUserName());
			if(wbInfo.getTypeResId() == R.string.weibo_type_cast){
				wbType.setText(context.getString(R.string.weibo_type_cast));
			}
			wbTime.setText(wbInfo.getTime());
			wbText.setText(wbInfo.getText(), TextView.BufferType.SPANNABLE);
			WeibogoUtility.textHighlight(wbText,'#', '#');
			WeibogoUtility.textHighlight(wbText,'@',':');
			WeibogoUtility.textHighlight(wbText,'@',' ');
			WeibogoUtility.textHighlight2(wbText,"http://"," ");
			if(wbInfo.getHaveImage()){
	              wbImage.setImageResource(R.drawable.imageprompticon);
	        }
            Drawable cachedImage = asyncImageLoader.loadDrawable(wbInfo.getUserIcon(), "50", wbIcon, new ImageCallback(){

                @Override
                public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl, View progressIndicator) {
                    imageView.setImageDrawable(imageDrawable);
                }
                
            }, null);
			if (cachedImage != null) {
				wbIcon.setImageDrawable(cachedImage);
			}else{
				wbIcon.setImageResource(android.R.drawable.sym_def_app_icon);
			}
			if(wbInfo.isHasSource()){
				//source weibo
				wbSourceLayout.setVisibility(View.VISIBLE);
				String textStr = wbInfo.getSource().getUserName().concat(":").concat(wbInfo.getSource().getText());
				wbSourceText.setText(textStr);
				if(wbInfo.getSource().getHaveImage()){
					wbSourceImage.setImageResource(R.drawable.imageprompticon);
				}
				textStr = wbInfo.getSource().getTime().concat(" ").concat(wbInfo.getSource().getFrom());
				wbSourceTimeAndFromText.setText(textStr);
			}
		}
		return convertView;
	}
}
